package com.smartcommunity.controller.access;

import com.smartcommunity.bean.entity.User;
import com.smartcommunity.bean.model.Result;
import com.smartcommunity.controller.BaseController;
import com.smartcommunity.elasticsearch.accesscontrol.AccesscontrolEsApi;
import com.smartcommunity.elasticsearch.accesscontrol.req.AccesscontrolEsReq;
import com.smartcommunity.elasticsearch.entranceguard.EntranceGuardEsApi;
import com.smartcommunity.elasticsearch.entranceguard.req.EntranceGuardEsReq;
import com.smartcommunity.log.interceptor.Log;
import com.smartcommunity.service.CommonService;
import com.smartcommunity.utils.ArrayUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.map.HashedMap;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@Api(value = "门禁记录API", tags = "门禁通行记录")
@RequestMapping("/access")
public class EntranceGuardController extends BaseController {

    @Autowired
    private EntranceGuardEsApi entranceGuardEsApi;


    @Autowired
    private AccesscontrolEsApi accesscontrolEsApi;


    @Autowired
    private CommonService commonService;

    /**
     * 门禁记录信息
     *
     * @param
     * @return
     */
    @Log(info = "查询门禁记录列表",module = "门禁管理",function = "查询")
    @PostMapping(value = "/getEntranceList")
    @ApiOperation(value = "门禁记录信息", notes = "门禁记录信息")
    public Result getEntranceList(@Valid @RequestBody EntranceGuardEsReq entranceGuardEsReq) {
        if (StringUtils.isBlank(entranceGuardEsReq.getVillageCode())) {
            User user = getCurrentUser();
            String villageCode = commonService.getVillageCodeByUser(user);
            if (StringUtils.isNotEmpty(entranceGuardEsReq.getParentId())) {
                String orgIds = commonService.getAllChildOrgId(entranceGuardEsReq.getParentId());
                Map<String, Object> map = new HashedMap();
                map.put("orgId", orgIds);
                map.put("villageCode", villageCode);
                List<String> villageList = commonService.listVillageCodeByOrgId(map);
                //此行政区划节点下没有小区，则返回
                if (CollectionUtils.isEmpty(villageList)){
                    return Result.ok();
                }
                villageCode = ArrayUtils.toString(villageList);
            }
            entranceGuardEsReq.setVillageCode(villageCode.replaceAll("'", ""));
        }
        return Result.ok(entranceGuardEsApi.getEntranceList(entranceGuardEsReq));

    }


    @PostMapping(value = "/searchCaptureAccesscontrol")
    @ApiOperation(value = "查询门禁抓拍信息", notes = "查询门禁抓拍信息")
    public List<Map<String, Object>> searchRecentlyCaptureAccesscontrol(@Valid @RequestBody AccesscontrolEsReq accesscontrolEsReq) {
        return accesscontrolEsApi.searchRecentlyCaptureAccesscontrol(accesscontrolEsReq);
    }

    /**
     * 根据门禁ID查询最新的门禁记录
     *
     * @param
     * @return
     */
    @Log(info = "查询门禁记录列表",module = "门禁管理",function = "查询")
    @GetMapping(value = "/getLastAccessInfo")
    @ApiOperation(value = "根据门禁ID查询最新的门禁记录", notes = "根据门禁ID查询最新的门禁记录")
    public Result getLastAccessInfo(@RequestParam(required = false, name = "deviceId") String deviceId) {
        return Result.ok(entranceGuardEsApi.getLastAccessInfo(deviceId));
    }


}
